﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Project.DAL.System;
using System.Dynamic;
using Newtonsoft.Json;
using Project.MODEL;

namespace Project.BLL.System
{
    public class MenusBll
    {
        MenusDal dal = new MenusDal();
        /// <summary>
        /// 获取所有菜单列表
        /// </summary>
        /// <returns></returns>
        public string GetMenusAll()
        {
            var menus = dal.GetMenus();

            dynamic d = new ExpandoObject();

            var list = from m in menus
                       select new
                       {
                           m.menuID,
                           m.menuName,
                           m.menuUrl,
                           m.areaName,
                           m.controllerName,
                           m.actionName,
                           m.parentID,
                           m.icon
                       };

            int count = list.Count();
            d.code = count > 0 ? 1 : 0;
            d.msg = count > 0 ? "菜单数据" : "";
            d.data = list;

            return JsonConvert.SerializeObject(d);
        }

        /// <summary>
        /// 添加模块
        /// </summary>
        /// <param name="menu"></param>
        /// <returns></returns>
        public string AddModular(Menus menu)
        {
            //统一定义返回数据格式
            dynamic data = new ExpandoObject();
            menu.areaName = "";
            menu.controllerName = "";
            menu.actionName = "";
            menu.parentID = 0;

            int num = dal.AddMenus(menu);
            data.code = num;
            data.msg = num > 0 ? "添加成功！" : "添加失败！";
            return JsonConvert.SerializeObject(data);
        }

        /// <summary>
        /// 添加菜单
        /// </summary>
        /// <param name="menu"></param>
        /// <returns></returns>
        public string AddMenus(Menus menu)
        {
            //统一定义返回数据格式
            dynamic data = new ExpandoObject();
            int num = dal.AddMenus(menu);
            data.code = num;
            data.msg = num > 0 ? "添加成功！" : "添加失败！";
            return JsonConvert.SerializeObject(data);
        }

        /// <summary>
        /// 修改菜单
        /// </summary>
        /// <param name="menu"></param>
        /// <returns></returns>
        public string UpdateMenus(Menus menu)
        {

            int num = dal.UpdateMenus(menu);

            //统一定义返回数据格式
            dynamic data = new ExpandoObject();
            data.code = num;
            data.msg = num > 0 ? "修改成功！" : "修改失败！";

            return JsonConvert.SerializeObject(data);
        }

        /// <summary>
        /// 删除菜单
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public string DeleteByID(int id)
        {
            var datas = dal.GetMenus();
            var menu = datas.Where(d => d.menuID == id).FirstOrDefault();
            //统一定义返回数据格式
            dynamic data = new ExpandoObject();
            if (menu.Authority.Count > 0)
            {
                data.code = -1;
                data.msg = "该菜单正在使用中无法删除！";
            }
            else
            {
                int num = dal.DeleteMenus(menu);
                data.code = num;
                data.msg = num > 0 ? "删除成功！" : "删除失败！";
            }
            return JsonConvert.SerializeObject(data);
        }

        /// <summary>
        /// 修改菜单（单项）
        /// </summary>
        /// <param name="id"></param>
        /// <param name="field"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public string UpdateMenus(int id, string field, string value)
        {
            var datas = dal.GetMenus();
            var menu = datas.Where(m => m.menuID == id).FirstOrDefault();

            //统一定义返回数据格式
            dynamic data = new ExpandoObject();
            if (menu==null)
            {
                data.code = -1;
                data.msg = "修改失败,参数异常！";

            }
            else
            {
                switch (field)
                {
                    case "areaName":
                        menu.areaName = value;
                        break;
                    case "controllerName":
                        menu.controllerName = value;
                        break;
                    case "actionName":
                        menu.actionName = value;
                        break;
                    default:
                        break;
                }
                menu.menuUrl = "/" + menu.areaName + "/" + menu.controllerName + "/" + menu.actionName;
                int num = dal.UpdateMenus(menu);
                data.code = num;
                data.msg = num > 0 ? "修改成功！" : "修改失败！";
            }

           

            return JsonConvert.SerializeObject(data);
        }

        /// <summary>
        /// 获取菜单--权限 
        /// </summary>
        /// <param name="rid"></param>
        /// <returns></returns>
        public string GetMenusAuthority(int rid) {
            var menus = dal.GetMenus();
            var anths = dal.GetAuthorities();

            //左外连接查询 菜单与角色权限
            var list = from m in menus
                       join a in anths.Where(a=>a.roleID==rid)
                       on m.menuID equals a.menuID into ma
                       from am in ma.DefaultIfEmpty()
                       select new {
                           id = m.menuID,
                           pid = m.parentID,
                           title = m.menuName,
                           icon = m.icon,
                           rid = rid,
                           check = am!=null?true:false
                       };


            return JsonConvert.SerializeObject(list);

        }


        /// <summary>
        /// 设置菜单--权限 
        /// </summary>
        /// <param name="rid"></param>
        /// <returns></returns>
        public string SetMenusAuthority(int rid,int[] mids)
        {
            //获取原有权限
            var oldauth = dal.GetAuthorities().Where(a => a.roleID==rid).ToList();

            //筛选出没有包含的旧权限（需要删除的）
            var relist = oldauth.Where(a => !mids.Contains(a.menuID)).ToList();

            //筛选出没有包含的新权限mid（需要新增的）
            var newmid = mids.Where(m => !oldauth.Select(a => a.menuID).Contains(m));

            //循环添加新的权限菜单
            var newlist = new List<Authority>();
            foreach (var i in newmid)
            {
                //记录新添加的权限
                newlist.Add(new Authority()
                {
                    menuID = i,
                    roleID = rid,
                    sort = 0
                });
            }

            //统一定义返回数据格式
            dynamic data = new ExpandoObject();

            //如果跟原有权限没有变动
            if (newlist.Count==0 && relist.Count()==0)
            {
                data.code = 1;
                data.msg = "没有任何变动!";
            }
            else
            {
                int num = dal.SetAuthorities(relist, newlist);
                data.code = num;
                data.msg = num > 0 ? "设置成功!" : "设置失败!";
            }
            return JsonConvert.SerializeObject(data);

        }

    }
}
